import pandas as pd
import rqdatac as rq
rq.init("13570866213", "39314656")
start_date = "2021-04-08"
end_date = "2021-04-08"
start_ins = rq.all_instruments(type="Future", date=start_date)
start_ins_2 = start_ins.loc[start_ins["de_listed_date"]!="0000-00-00"]
end_ins = rq.all_instruments(type="Future", date=end_date)
end_ins_2 = end_ins.loc[end_ins["de_listed_date"]!="0000-00-00"]
all_ins = pd.concat([start_ins_2, end_ins_2])
all_ins.drop_duplicates("order_book_id", inplace=True)
#all_ins.reset_index(inplace=True)
all_ins.set_index("order_book_id", inplace=True)
all_ins.loc[all_ins["exchange"].isin(["DCE", "SHFE", "INS"]), "underlying_symbol"] = all_ins["underlying_symbol"].str.lower()
all_ins["vt_symbol"] = all_ins["trading_code"] + "." + all_ins["exchange"]
all_ins["index_symbol"] = all_ins["underlying_symbol"] + "99." + all_ins["exchange"]

index_symbol_dict = all_ins["index_symbol"].to_dict()
vt_symbol_dict = all_ins["vt_symbol"].to_dict()
symbol_dict = all_ins["trading_code"].to_dict()

all_data = rq.get_price(all_ins.index.to_list(), start_date, end_date, "1d", expect_df=True)
all_data.reset_index(drop=False, inplace=True)
all_data["vt_symbol"] = all_data["order_book_id"].map(vt_symbol_dict)
all_data["index_symbol"] = all_data["order_book_id"].map(index_symbol_dict)
all_data["symbol"] = all_data["order_book_id"].map(symbol_dict)
date_list = all_data["date"].drop_duplicates().dt.strftime("%Y%m%d").to_list()
date_list.sort()

